package com.jf.yytgd.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf.yytgd.entity.dto.combined.DiaryUserBarDTO;
import com.jf.yytgd.entity.dto.combined.DiaryUserDTO;
import com.jf.yytgd.entity.dto.comment.CommentCreateDTO;
import com.jf.yytgd.entity.dto.diary.DiaryPublishDTO;
import com.jf.yytgd.entity.dto.like.LikeDTO;
import com.jf.yytgd.entity.dto.like.LikeExistDTO;
import com.jf.yytgd.service.DiaryService;
import com.jf.yytgd.utils.Msg;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 * Description: 日记控制层
 * User:22954
 * Date:2025-01-22
 * Time:20:10
 */
@RequestMapping("/diary")
@RestController
public class DiaryController {
    @Resource
    private DiaryService diaryService;

    @GetMapping("/list")
    public Msg list(@RequestParam(value = "page_no", defaultValue = "1") Integer pageNo,
                    @RequestParam(value = "page_Size", defaultValue = "15") Integer pageSize,
                    @RequestParam(value = "search", required = false) String title,
                    @RequestParam(value = "order", required = false) String order) {
        Page<DiaryUserBarDTO> page = diaryService.barPage(pageNo, pageSize, title, order);
        Msg msg = Msg.success();
        msg.addData("diaries", page);
        return msg;
    }

    @GetMapping("/userList")
    public Msg userList(@RequestParam(value = "page_no", defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "page_size", defaultValue = "15") Integer pageSize,
                        @RequestParam("user_id") Integer userId) {
        Page<DiaryUserBarDTO> page = diaryService.userBarPage(pageNo, pageSize, userId);
        Msg msg = Msg.success();
        msg.addData("diaries", page);
        return msg;
    }

    @GetMapping
    public Msg get(@RequestParam("id") Integer id) {
        DiaryUserDTO diary = diaryService.browse(id);
        Msg msg = Msg.success();
        msg.addData("diary", diary);
        return msg;
    }

    @PostMapping("/publish")
    public Msg save(@Validated @RequestBody DiaryPublishDTO diary) {
        diaryService.publish(diary);
        return Msg.success();
    }

    @PutMapping("/like")
    public Msg like(@RequestBody @Validated LikeDTO like) {
        diaryService.like(like);
        return Msg.success();
    }

    @PostMapping("/like/exist")
    public Msg isLike(@RequestBody @Validated LikeExistDTO like) {
        boolean isLike = diaryService.isLike(like);
        Msg msg = Msg.success();
        msg.addData("isLike", isLike);
        return msg;
    }
    @PostMapping("/comment")
    public Msg comment(@RequestBody @Validated CommentCreateDTO comment) {
        diaryService.comment(comment);
        return Msg.success();
    }
    @GetMapping("/comments")
    public Msg commentList(@RequestParam("diary_id") Integer diaryId) {
        Msg msg = Msg.success();
        msg.addData("comments", diaryService.listComment(diaryId));
        return msg;
    }
}
